package com.egao.appo.biz.impl;

import com.egao.appo.entity.Information;
import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.PinyinUtils;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.system.entity.User;
import com.egao.appo.biz.AppointmentsBiz;
import com.egao.appo.entity.Appointments;
import com.egao.common.core.web.JsonResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 * 数据层实现类
 *
 * @author : Doman
 * @version ：1.0
 * @date : 2021-07-20 09:09:53
 */
@Repository
public class AppointmentsBizImpl extends BaseDao implements AppointmentsBiz {

  @Override
  public JsonResult list(PageResult2<T> page, Appointments appointments, User loginUser) throws Exception {
    Map<String, String> map = new HashMap<String, String>();
    RowMapper<Appointments> rm = new BeanPropertyRowMapper<Appointments>(Appointments.class);
    StringBuffer sql = new StringBuffer("select a.id,a.organization_id,a.name,a.b_code");
    sql.append(",a.appo_user_id,a.match_maker_id");
    sql.append(",a.pinyin,a.gender,a.phone,a.id_card_pic");
    sql.append(",a.height,a.weight,a.education,a.occupation");
    sql.append(",a.salary,a.house,a.car,a.self_situation,a.only_son,a.parents_situation");
    sql.append(",a.hobby,a.requirements,a.birthday,a.marriage");
    sql.append(",a.native_place,a.live_city,a.native_place_v,a.live_city_v,a.wechat_code,a.id_card");
    sql.append(",a.labeled,a.examined,a.id_card_pic_b");
    sql.append(",a.education_pic,a.order_num,a.create_time");
    sql.append(",a.photo,a.show_photo");
    sql.append(",a.photo_privacy");
    sql.append(",a.create_user,a.update_time,a.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.username createName ");
    sql.append(",suser2.username updateName ");
    sql.append(" from fx_appointments a ");
    sql.append(" left join sys_user suser on suser.user_id=a.create_user ");
    sql.append(" left join sys_user suser2 on suser2.user_id=a.update_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=a.organization_id and sogzn.deleted=0 ");
    sql.append(" where a.deleted=0 ");
    if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
      sql.append(" and a.organization_id=:tableOrganizationId ");
      map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
    }
    if (Utils.isNotEmpty(appointments)) {
      if (Utils.isNotEmpty(appointments.getName())) {
        sql.append(" and (instr(a.name,:name)>0 or instr(a.pinyin,:name)>0 or instr(a.b_code,:name)>0) ");
        map.put("name", appointments.getName()+"");
      }
      if (Utils.isNotEmpty(appointments.getGender())) {
        sql.append(" and a.gender=:gender ");
        map.put("gender", appointments.getGender()+"");
      }
      if (Utils.isNotEmpty(appointments.getBirthday())) {
        if (appointments.getBirthday().equals("00-05")) {
          sql.append(" and (a.birthday>='2000-01-01 00:00:00' and a.birthday<'2005-01-01 00:00:00') ");
        } else if (appointments.getBirthday().equals("95-00")) {
          sql.append(" and (a.birthday>='1995-01-01 00:00:00' and a.birthday<'2000-01-01 00:00:00') ");
        } else if (appointments.getBirthday().equals("90-95")) {
          sql.append(" and (a.birthday>='1990-01-01 00:00:00' and a.birthday<'1995-01-01 00:00:00') ");
        } else if (appointments.getBirthday().equals("85-90")) {
          sql.append(" and (a.birthday>='1985-01-01 00:00:00' and a.birthday<'1990-01-01 00:00:00') ");
        } else if (appointments.getBirthday().equals("80-85")) {
          sql.append(" and (a.birthday>='1980-01-01 00:00:00' and a.birthday<'1985-01-01 00:00:00') ");
        } else if (appointments.getBirthday().equals("'其他'")) {
          sql.append(" and (a.birthday>='2006-01-01 00:00:00' or a.birthday<'1980-01-01 00:00:00') ");
        } else {
          sql.append(" and (a.birthday>'2006-01-01 00:00:00' or a.birthday<'1980-01-01 00:00:00') ");
        }
      }
      if (Utils.isNotEmpty(appointments.getMarriage())) {
        sql.append(" and instr(a.marriage,:marriage)>0 ");
        map.put("marriage", appointments.getMarriage());
      }
      if (Utils.isNotEmpty(appointments.getLabeled())) {
        if (appointments.getLabeled().equals("有")) {
          sql.append(" and a.labeled!='' and a.labeled!='null' ");
        } else{
          sql.append(" and instr(a.labeled,:labeled)>0 ");
          map.put("labeled", appointments.getLabeled());
        }
      }
      if (Utils.isNotEmpty(appointments.getShowPhoto())) {
        sql.append(" and a.show_photo=:show_photo ");
        map.put("show_photo", appointments.getShowPhoto()+"");
      }
      if (Utils.isNotEmpty(appointments.getExamined())) {
        sql.append(" and a.examined=:examined ");
        map.put("examined", appointments.getExamined());
      }
      if (Utils.isNotEmpty(appointments.getEducation())) {
        sql.append(" and a.education=:education ");
        map.put("education", appointments.getEducation());
      }
      if (Utils.isNotEmpty(appointments.getSort()) && Utils.isNotEmpty(appointments.getOrder())) {
        sql.append(" order by a." + appointments.getSort() + " " + appointments.getOrder());
      } else {
        sql.append(" order by a.show_photo desc,a.update_time desc,a.create_time desc");
      }
    }
    return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
  }

  @Override
  public JsonResult list2(PageResult2<T> page, Appointments appointments, User loginUser) throws Exception {
    Map<String, String> map = new HashMap<String, String>();
    RowMapper<Appointments> rm = new BeanPropertyRowMapper<Appointments>(Appointments.class);
    StringBuffer sql = new StringBuffer("select a.id,a.organization_id,a.name,a.b_code");
    sql.append(",a.appo_user_id,a.match_maker_id");
    sql.append(",a.pinyin,a.gender,a.phone,a.id_card_pic");
    sql.append(",a.height,a.weight,a.education,a.occupation");
    sql.append(",a.salary,a.house,a.car,a.self_situation,a.only_son,a.parents_situation");
    sql.append(",a.hobby,a.requirements,a.birthday,a.marriage");
    sql.append(",a.native_place,a.live_city,a.native_place_v,a.live_city_v,a.wechat_code,a.id_card");
    sql.append(",a.labeled,a.examined,a.id_card_pic_b");
    sql.append(",a.education_pic,a.order_num,a.create_time");
    sql.append(",a.photo");
    sql.append(",a.photo_privacy");
    sql.append(",a.create_user,a.update_time,a.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.username createName ");
    sql.append(",suser2.username updateName ");
    sql.append(" from fx_appointments a ");
    sql.append(" left join sys_user suser on suser.user_id=a.create_user ");
    sql.append(" left join sys_user suser2 on suser2.user_id=a.update_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=a.organization_id and sogzn.deleted=0 ");
    sql.append(" where a.deleted=0 ");
    if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
      sql.append(" and a.organization_id=:tableOrganizationId ");
      map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
    }
    if (Utils.isNotEmpty(appointments)) {
      if (Utils.isNotEmpty(appointments.getName())) {
        sql.append(" and (instr(a.name,:name)>0 or instr(a.pinyin,:name)>0 or instr(a.b_code,:name)>0) ");
        map.put("name", appointments.getName()+"");
      }
      if (Utils.isNotEmpty(appointments.getGender())) {
        sql.append(" and a.gender=:gender ");
        map.put("gender", appointments.getGender()+"");
      }
      if (Utils.isNotEmpty(appointments.getBirthday())) {
        if (appointments.getBirthday().equals("00-05")) {
          sql.append(" and (a.birthday>='2000-01-01 00:00:00' and a.birthday<'2005-01-01 00:00:00') ");
        } else if (appointments.getBirthday().equals("95-00")) {
          sql.append(" and (a.birthday>='1995-01-01 00:00:00' and a.birthday<'2000-01-01 00:00:00') ");
        } else if (appointments.getBirthday().equals("90-95")) {
          sql.append(" and (a.birthday>='1990-01-01 00:00:00' and a.birthday<'1995-01-01 00:00:00') ");
        } else if (appointments.getBirthday().equals("85-90")) {
          sql.append(" and (a.birthday>='1985-01-01 00:00:00' and a.birthday<'1990-01-01 00:00:00') ");
        } else if (appointments.getBirthday().equals("80-85")) {
          sql.append(" and (a.birthday>='1980-01-01 00:00:00' and a.birthday<'1985-01-01 00:00:00') ");
        } else if (appointments.getBirthday().equals("'其他'")) {
          sql.append(" and (a.birthday>='2006-01-01 00:00:00' or a.birthday<'1980-01-01 00:00:00') ");
        } else {
          sql.append(" and (a.birthday>'2006-01-01 00:00:00' or a.birthday<'1980-01-01 00:00:00') ");
        }
      }
      if (Utils.isNotEmpty(appointments.getMarriage())) {
        sql.append(" and instr(a.marriage,:marriage)>0 ");
        map.put("marriage", appointments.getMarriage());
      }
      if (Utils.isNotEmpty(appointments.getLabeled())) {
        if (appointments.getLabeled().equals("有")) {
          sql.append(" and a.labeled!='' and a.labeled!='null' ");
        } else{
          sql.append(" and instr(a.labeled,:labeled)>0 ");
          map.put("labeled", appointments.getLabeled());
        }
      }
      if (Utils.isNotEmpty(appointments.getShowPhoto())) {
        sql.append(" and a.show_photo=:show_photo ");
        map.put("show_photo", appointments.getShowPhoto()+"");
      }
      if (Utils.isNotEmpty(appointments.getExamined())) {
        sql.append(" and a.examined=:examined ");
        map.put("examined", appointments.getExamined());
      }
      if (Utils.isNotEmpty(appointments.getEducation())) {
        sql.append(" and a.education=:education ");
        map.put("education", appointments.getEducation());
      }
      if (Utils.isNotEmpty(appointments.getSort()) && Utils.isNotEmpty(appointments.getOrder())) {
        sql.append(" order by a." + appointments.getSort() + " " + appointments.getOrder());
      } else {
        sql.append(" order by a.create_time desc");
      }
    }
    return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
  }

  @Override
  public List<Appointments> list(Appointments appointments, User loginUser) {
    Map<String, String> map = new HashMap<String, String>();
    RowMapper<Appointments> rm = new BeanPropertyRowMapper<Appointments>(Appointments.class);
    StringBuffer sql = new StringBuffer("select a.id,a.organization_id,a.name,a.b_code");
    sql.append(",a.appo_user_id,a.match_maker_id");
    sql.append(",a.pinyin,a.gender,a.phone,a.id_card_pic");
    sql.append(",a.height,a.weight,a.education,a.occupation");
    sql.append(",a.salary,a.house,a.car,a.self_situation,a.only_son,a.parents_situation");
    sql.append(",a.hobby,a.requirements,a.birthday,a.marriage");
    sql.append(",a.native_place,a.live_city,a.native_place_v,a.live_city_v,a.wechat_code,a.id_card");
    sql.append(",a.labeled,a.examined,a.id_card_pic_b");
    sql.append(",a.education_pic,a.order_num,a.create_time");
    sql.append(",a.photo,a.show_photo");
    sql.append(",a.photo_privacy");
    sql.append(",a.create_user,a.update_time,a.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.username createName ");
    sql.append(",suser2.username updateName ");
    sql.append(" from fx_appointments a ");
    sql.append(" inner join sys_user suser on suser.user_id=a.create_user ");
    sql.append(" inner join sys_user suser2 on suser2.user_id=a.update_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=a.organization_id and sogzn.deleted=0 ");
    sql.append(" where a.deleted=0 ");
    if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
      sql.append(" and a.organization_id=:tableOrganizationId ");
      map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
    }
    if (Utils.isNotEmpty(appointments)) {
      if (Utils.isNotEmpty(appointments.getName())) {
        sql.append(" and (instr(a.name,:name)>0 or instr(a.pinyin,:name)>0 or instr(a.b_code,:name)>0) ");
        map.put("name", appointments.getName()+"");
      }
      if (Utils.isNotEmpty(appointments.getGender())) {
        sql.append(" and a.gender=:gender ");
        map.put("gender", appointments.getGender()+"");
      }
      if (Utils.isNotEmpty(appointments.getBirthday())) {
        if (appointments.getBirthday().equals("00-05")) {
          sql.append(" and (a.birthday>='2000-01-01 00:00:00' and a.birthday<'2005-01-01 00:00:00') ");
        } else if (appointments.getBirthday().equals("95-00")) {
          sql.append(" and (a.birthday>='1995-01-01 00:00:00' and a.birthday<'2000-01-01 00:00:00') ");
        } else if (appointments.getBirthday().equals("90-95")) {
          sql.append(" and (a.birthday>='1990-01-01 00:00:00' and a.birthday<'1995-01-01 00:00:00') ");
        } else if (appointments.getBirthday().equals("85-90")) {
          sql.append(" and (a.birthday>='1985-01-01 00:00:00' and a.birthday<'1990-01-01 00:00:00') ");
        } else if (appointments.getBirthday().equals("80-85")) {
          sql.append(" and (a.birthday>='1980-01-01 00:00:00' and a.birthday<'1985-01-01 00:00:00') ");
        } else if (appointments.getBirthday().equals("'其他'")) {
          sql.append(" and (a.birthday>='2006-01-01 00:00:00' or a.birthday<'1980-01-01 00:00:00') ");
        } else {
          sql.append(" and (a.birthday>'2006-01-01 00:00:00' or a.birthday<'1980-01-01 00:00:00') ");
        }
      }
      if (Utils.isNotEmpty(appointments.getMarriage())) {
        sql.append(" and instr(a.marriage,:marriage)>0 ");
        map.put("marriage", appointments.getMarriage());
      }
      if (Utils.isNotEmpty(appointments.getLabeled())) {
        if (appointments.getLabeled().equals("有")) {
          sql.append(" and a.labeled!='' and a.labeled!='null' ");
        } else{
          sql.append(" and instr(a.labeled,:labeled)>0 ");
          map.put("labeled", appointments.getLabeled());
        }
      }
      if (Utils.isNotEmpty(appointments.getName())) {
        sql.append(" and (instr(a.name,:name)>0 or instr(a.pinyin,:name)>0)");
        map.put("name", appointments.getName());
      }
      if (Utils.isNotEmpty(appointments.getShowPhoto())) {
        sql.append(" and a.show_photo=:show_photo ");
        map.put("show_photo", appointments.getShowPhoto()+"");
      }
      if (Utils.isNotEmpty(appointments.getExamined())) {
        sql.append(" and a.examined=:examined ");
        map.put("examined", appointments.getExamined());
      }
      if (Utils.isNotEmpty(appointments.getEducation())) {
        sql.append(" and a.education=:education ");
        map.put("education", appointments.getEducation());
      }
      if (Utils.isNotEmpty(appointments.getSort()) && Utils.isNotEmpty(appointments.getOrder())) {
        sql.append(" order by a." + appointments.getSort() + " " + appointments.getOrder());
      } else {
        sql.append(" order by a.create_time desc");
      }
    }
    getMapParameter(sql, map);
    return jdbcTemplate.query(sql.toString(), map, rm);
  }

  @Override
  public List<Appointments> findListId() {
    Map<String, String> map  = new HashMap<String, String>();
    RowMapper<Appointments> rm = new BeanPropertyRowMapper<Appointments>(Appointments.class);
    StringBuffer sql = new StringBuffer("select i.id");
    sql.append(" from fx_appointments i ");
    sql.append(" where deleted=0 ");
    sql.append(" and salary is not null and salary!='' and salary!='null' ");
    sql.append(" and self_situation is not null and self_situation!='' and self_situation!='null' ");
    sql.append(" and parents_situation is not null and parents_situation!='' and parents_situation!='null' ");
    sql.append(" and native_place_v is not null and native_place_v!='' and native_place_v!='null' ");
    sql.append(" and live_city_v is not null and live_city_v!='' and live_city_v!='null' ");
    sql.append(" and examined='已审核' ");
    getMapParameter(sql, map);
    return jdbcTemplate.query(sql.toString(),map, rm);
  }

  @Override
  public List<Information> informationList() {
    Map<String, String> map  = new HashMap<String, String>();
    RowMapper<Information> rm = new BeanPropertyRowMapper<Information>(Information.class);
    StringBuffer sql = new StringBuffer("select i.id,i.organization_id,i.openid");
    sql.append(",i.userAvatar,i.userNickName");
    sql.append(",i.user_id,i.nickname,i.gender,i.age");
    sql.append(",i.phone,i.marriage,i.education,i.occupation");
    sql.append(",i.car,i.house,i.pic,i.height");
    sql.append(",i.weight,i.comment,i.nativeplace,i.address");
    sql.append(",i.hobby,i.requirement,i.type,i.state,i.reward");
    sql.append(",i.create_time,i.create_user,i.update_time,i.update_user,i.deleted");
    sql.append(" from a_information i ");
    getMapParameter(sql, map);
    return jdbcTemplate.query(sql.toString(),map, rm);
  }

  @Override
  public Appointments getById(Appointments appointments) {
    RowMapper<Appointments> rm = new BeanPropertyRowMapper<Appointments>(Appointments.class);
    Map<String, String> map = new HashMap<String, String>();
    StringBuffer sql = new StringBuffer("select a.id,a.organization_id,a.name,a.b_code");
    sql.append(",a.appo_user_id,a.match_maker_id");
    sql.append(",a.pinyin,a.gender,a.phone,a.id_card_pic");
    sql.append(",a.height,a.weight,a.education,a.occupation");
    sql.append(",a.salary,a.house,a.car,a.self_situation,a.only_son,a.parents_situation");
    sql.append(",a.hobby,a.requirements,a.birthday,a.marriage");
    sql.append(",a.native_place,a.live_city,a.native_place_v,a.live_city_v,a.wechat_code,a.id_card");
    sql.append(",a.labeled,a.examined,a.id_card_pic_b");
    sql.append(",a.education_pic,a.order_num,a.create_time");
    sql.append(",a.photo,a.show_photo");
    sql.append(",a.photo_privacy");
    sql.append(",a.create_user,a.update_time,a.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.username createName ");
    sql.append(",suser2.username updateName ");
    sql.append(" from fx_appointments a ");
    sql.append(" left join sys_user suser on suser.user_id=a.create_user ");
    sql.append(" left join sys_user suser2 on suser2.user_id=a.update_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=a.organization_id and sogzn.deleted=0 ");
    sql.append(" where a.deleted=0 and a.id=:id ");
    map.put("id", appointments.getId());
    getMapParameter(sql, map);
    return jdbcTemplate.queryForObject(sql.toString(), map, rm);
  }

  @Override
  public Appointments infoByCode(String b_code) {
    RowMapper<Appointments> rm = new BeanPropertyRowMapper<Appointments>(Appointments.class);
    Map<String, String> map = new HashMap<String, String>();
    StringBuffer sql = new StringBuffer("select a.id,a.organization_id,a.name,a.b_code");
    sql.append(",a.appo_user_id,a.match_maker_id");
    sql.append(",a.pinyin,a.gender,a.phone,a.id_card_pic");
    sql.append(",a.height,a.weight,a.education,a.occupation");
    sql.append(",a.salary,a.house,a.car,a.self_situation,a.only_son,a.parents_situation");
    sql.append(",a.hobby,a.requirements,a.birthday,a.marriage");
    sql.append(",a.native_place,a.live_city,a.native_place_v,a.live_city_v,a.wechat_code,a.id_card");
    sql.append(",a.labeled,a.examined,a.id_card_pic_b");
    sql.append(",a.education_pic,a.order_num,a.create_time");
    sql.append(",a.photo,a.show_photo");
    sql.append(",a.photo_privacy");
    sql.append(",a.create_user,a.update_time,a.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.username createName ");
    sql.append(",suser2.username updateName ");
    sql.append(" from fx_appointments a ");
    sql.append(" left join sys_user suser on suser.user_id=a.create_user ");
    sql.append(" left join sys_user suser2 on suser2.user_id=a.update_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=a.organization_id and sogzn.deleted=0 ");
    sql.append(" where a.deleted=0 and a.b_code=:b_code ");
    map.put("b_code", b_code);
    getMapParameter(sql, map);
    return jdbcTemplate.queryForObject(sql.toString(), map, rm);
  }


  @Override
  public Appointments getByAppoUserId(String appoUserId) {
    RowMapper<Appointments> rm = new BeanPropertyRowMapper<Appointments>(Appointments.class);
    Map<String, String> map = new HashMap<String, String>();
    StringBuffer sql = new StringBuffer("select a.id,a.organization_id,a.name,a.b_code");
    sql.append(",a.appo_user_id,a.match_maker_id");
    sql.append(",a.pinyin,a.gender,a.phone,a.id_card_pic");
    sql.append(",a.height,a.weight,a.education,a.occupation");
    sql.append(",a.salary,a.house,a.car,a.self_situation,a.only_son,a.parents_situation");
    sql.append(",a.hobby,a.requirements,a.birthday,a.marriage");
    sql.append(",a.native_place,a.live_city,a.native_place_v,a.live_city_v,a.wechat_code,a.id_card");
    sql.append(",a.labeled,a.examined,a.id_card_pic_b");
    sql.append(",a.education_pic,a.order_num,a.create_time");
    sql.append(",a.photo,a.show_photo");
    sql.append(",a.photo_privacy");
    sql.append(",a.create_user,a.update_time,a.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.username createName ");
    sql.append(",suser2.username updateName ");
    sql.append(" from fx_appointments a ");
    sql.append(" left join sys_user suser on suser.user_id=a.create_user ");
    sql.append(" left join sys_user suser2 on suser2.user_id=a.update_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=a.organization_id and sogzn.deleted=0 ");
    sql.append(" where a.deleted=0 and a.appo_user_id=:appo_user_id ");
    map.put("appo_user_id", appoUserId);
    getMapParameter(sql, map);
    return jdbcTemplate.queryForObject(sql.toString(), map, rm);
  }

  @Override
  public Integer save(Appointments appointments, User loginUser) {
    appointments.setPinyin(PinyinUtils.ToFirstChar(appointments.getName()));
    GeneratedKeyHolder generatedKeyHolder = new GeneratedKeyHolder();
    KeyHolder keyHolder = new GeneratedKeyHolder();
    Map<String, String> map = new HashMap<String, String>();
    StringBuffer sql = new StringBuffer("insert into fx_appointments (");
    sql.append("organization_id,appo_user_id,match_maker_id,name,b_code,pinyin,gender");
    sql.append(",phone,id_card_pic,id_card_pic_b,height,weight");
    sql.append(",education,occupation,salary,house");
    sql.append(",car,self_situation,only_son,parents_situation,hobby,requirements");
    sql.append(",birthday,marriage,native_place,live_city,native_place_v,live_city_v");
    sql.append(",wechat_code,id_card,labeled,education_pic,photo,show_photo");
    sql.append(",photo_privacy");
    sql.append(",order_num,create_user,create_time) values ( ");
    sql.append(":organization_id,:appo_user_id,:match_maker_id,:name,:b_code,:pinyin,:gender");
    sql.append(",:phone,:id_card_pic,:id_card_pic_b,:height,:weight");
    sql.append(",:education,:occupation,:salary,:house");
    sql.append(",:car,:self_situation,:only_son,:parents_situation,:hobby,:requirements");
    sql.append(",:birthday,:marriage,:native_place,:live_city,:native_place_v,:live_city_v");
    sql.append(",:wechat_code,:id_card,:labeled,:education_pic,:photo,:show_photo");
    sql.append(",:photo_privacy");
    sql.append(",:order_num,:create_user,now())");
    map.put("organization_id", loginUser.getOrganizationId() + "");
    map.put("appo_user_id", appointments.getAppoUserId());
    map.put("match_maker_id", appointments.getMatchMakerId());
    map.put("name", appointments.getName());
    map.put("b_code", appointments.getbCode());
    map.put("pinyin", appointments.getPinyin());
    map.put("gender", appointments.getGender() + "");
    map.put("phone", appointments.getPhone());
    map.put("id_card_pic", appointments.getIdCardPic());
    map.put("id_card_pic_b", appointments.getIdCardPicB());
    map.put("height", appointments.getHeight()+"");
    map.put("weight", appointments.getWeight()+"");
    map.put("education", appointments.getEducation());
    map.put("occupation", appointments.getOccupation());
    map.put("salary", appointments.getSalary());
    map.put("house", appointments.getHouse());
    map.put("car", appointments.getCar());
    map.put("self_situation", appointments.getSelfSituation());
    map.put("only_son", appointments.getOnlySon() + "");
    map.put("parents_situation", appointments.getParentsSituation());
    map.put("hobby", appointments.getHobby());
    map.put("requirements", appointments.getRequirements());
    map.put("birthday", appointments.getBirthday());
    map.put("marriage", appointments.getMarriage());
    map.put("native_place", appointments.getNativePlace());
    map.put("live_city", appointments.getLiveCity());
    map.put("native_place_v", appointments.getNativePlaceV());
    map.put("live_city_v", appointments.getLiveCityV());
    map.put("wechat_code", appointments.getWechatCode());
    map.put("id_card", appointments.getIdCard());
    map.put("labeled", appointments.getLabeled());
    map.put("education_pic", appointments.getEducationPic());
    map.put("photo", appointments.getPhoto());
    map.put("show_photo", appointments.getShowPhoto()+"");
    map.put("photo_privacy", appointments.getPhotoPrivacy());
    map.put("order_num", appointments.getOrderNum() == null ? null : appointments.getOrderNum() + "");
    map.put("create_user", loginUser.getUserId() + "");
    getMapParameter(sql, map);
    //		System.err.println("受到影响的行数=" + retVal + ",
    int retVal = this.jdbcTemplate.update(sql.toString(), new SqlParameterSource() {
      @Override
      public boolean hasValue(String paramName) {
        return map.containsKey(paramName);
      }

      @Override
      public Object getValue(String paramName) throws IllegalArgumentException {
        return map.get(paramName);
      }
    }, generatedKeyHolder);
    Integer id = generatedKeyHolder.getKey().intValue();
    return id;
  }

  @Override
  public boolean update(Appointments appointments, User loginUser) {
    appointments.setPinyin(PinyinUtils.ToFirstChar(appointments.getName()));
    Map<String, String> map = new HashMap<String, String>();
    StringBuffer sql = new StringBuffer("update fx_appointments set ");
    sql.append("name=:name,pinyin=:pinyin,gender=:gender ");
    sql.append(",phone=:phone,id_card_pic=:id_card_pic,id_card_pic_b=:id_card_pic_b,height=:height ");
    sql.append(",labeled=:labeled,examined=:examined ");
    sql.append(",weight=:weight,education=:education,occupation=:occupation ");
    sql.append(",salary=:salary,house=:house,car=:car ,only_son=:only_son,parents_situation=:parents_situation ");
    sql.append(",self_situation=:self_situation,hobby=:hobby,requirements=:requirements ");
    sql.append(",birthday=:birthday,marriage=:marriage,native_place=:native_place ");
    sql.append(",live_city=:live_city,wechat_code=:wechat_code,id_card=:id_card ");
    sql.append(",native_place_v=:native_place_v,live_city_v=:live_city_v ");
    sql.append(",education_pic=:education_pic,photo=:photo,show_photo=:show_photo,photo_privacy=:photo_privacy,order_num=:order_num ");
    sql.append(",update_time=now(),update_user=:update_user where id=:id ");
    map.put("name", appointments.getName());
    map.put("pinyin", appointments.getPinyin());
    map.put("gender", appointments.getGender() + "");
    map.put("phone", appointments.getPhone());
    map.put("id_card_pic", appointments.getIdCardPic());
    map.put("id_card_pic_b", appointments.getIdCardPicB());
    map.put("height", appointments.getHeight()+"");
    map.put("weight", appointments.getWeight()+"");
    map.put("labeled", appointments.getLabeled());
    map.put("examined", appointments.getExamined());
    map.put("education", appointments.getEducation());
    map.put("occupation", appointments.getOccupation());
    map.put("salary", appointments.getSalary());
    map.put("house", appointments.getHouse());
    map.put("car", appointments.getCar());
    map.put("self_situation", appointments.getSelfSituation());
    map.put("only_son", appointments.getOnlySon() + "");
    map.put("parents_situation", appointments.getParentsSituation());
    map.put("hobby", appointments.getHobby());
    map.put("requirements", appointments.getRequirements());
    map.put("birthday", appointments.getBirthday());
    map.put("marriage", appointments.getMarriage());
    map.put("native_place", appointments.getNativePlace());
    map.put("live_city", appointments.getLiveCity());
    map.put("native_place_v", appointments.getNativePlaceV());
    map.put("live_city_v", appointments.getLiveCityV());
    map.put("wechat_code", appointments.getWechatCode());
    map.put("id_card", appointments.getIdCard());
    map.put("education_pic", appointments.getEducationPic());
    map.put("photo", appointments.getPhoto());
    map.put("show_photo", appointments.getShowPhoto()+"");
    map.put("photo_privacy", appointments.getPhotoPrivacy());
    map.put("order_num", appointments.getOrderNum() == null ? null : appointments.getOrderNum() + "");
    map.put("update_user", loginUser.getUserId() + "");
    map.put("id", appointments.getId());
    getMapParameter(sql, map);
    int count = jdbcTemplate.update(sql.toString(), map);
    return count > 0;
  }


  @Override
  public boolean updateExamined(Appointments appointments, User loginUser) {
    int count = 0;
    Map<String, String> map = null;
    if (appointments.getId().length() > 0) {
      for (String ID : appointments.getId().split(",")) {
        map = new HashMap<String, String>();
        if (ID == null || ID.equals("")) {
          break;
        }
        StringBuffer sql = new StringBuffer("update fx_appointments set ");
        sql.append("examined=:examined ");
        sql.append(",update_time=now(),update_user=:update_user where id=:id ");
        map.put("examined", appointments.getExamined());
        map.put("update_user", loginUser.getUserId() + "");
        map.put("id", ID);
        getMapParameter(sql, map);
        count = jdbcTemplate.update(sql.toString(), map);
      }
    }
    return count > 0;
  }

  @Override
  public boolean remove(Appointments appointments, User loginUser) {
    int count = 0;
    Map<String, String> map = null;
    if (appointments.getId().length() > 0) {
      for (String ID : appointments.getId().split(",")) {
        map = new HashMap<String, String>();
        if (ID == null || ID.equals("")) {
          break;
        }
        StringBuffer sql = new StringBuffer("update fx_appointments set deleted=1" +
                ",update_user=:update_user, update_time=now() "
                + " where id =:id ");
        map.put("update_user", loginUser.getUserId() + "");
        map.put("id", ID);
        getMapParameter(sql, map);
        count = jdbcTemplate.update(sql.toString(), map);
      }
    }
    return count > 0;
  }

}
